import pygame
import Image
from pygame.locals import *
from pathing import *

# Useful functions needed for the game

# load sprite image, return Surface
def image_load(name):
    image = pygame.image.load(name)
    image = image.convert()
    colorkey = image.get_at((0,0))
    image.set_colorkey(colorkey, RLEACCEL)
    return image

# load image, return Image
def image_load2(name):
    image = Image.open(name)
    return image    

def collision(pos_x, pos_y, obj_x, obj_y):
    if (pos_x > obj_x - 32) and (pos_x < obj_x + 32) and (pos_y > obj_y - 32) and (pos_y < obj_y + 32):
        return True
    else:
        return False

#http://stackoverflow.com/questions/1927660/compare-two-images-the-python-linux-way
def compare(image1path, image2path):
    import Image
    import math
    import operator
    __docstring__ = "compare two image files"
    h1 = Image.open(image1path).histogram()
    h2 = Image.open(image2path).histogram()
    rms = math.sqrt(reduce(operator.add, map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
    return rms == 0

def collide_edges(object_1, object_2):
    l, r, t, b, i = False, False, False, False, False
    Rect = pygame.Rect
##    left = Rect(object_1.left, object_1.top+1, 1, object_1.height-1)
##    right = Rect(object_1.right, object_1.top+1, 1, object_1.height-1)
##    top = Rect(object_1.left + 1, object_1.top, object_1.width-1, 1)
##    bottom = Rect(object_1.left + 1, object_1.bottom, object_1.width-1, 1)
##    inside = Rect(object_1.left + 1, object_1.top + 1, object_1.width - 2, object_1.height - 1)
    left = Rect(object_1.left, object_1.top+2, 2, object_1.height-2)
    right = Rect(object_1.right, object_1.top+2, 2, object_1.height-2)
    top = Rect(object_1.left + 4, object_1.top - 2, object_1.width-4, 4)
    bottom = Rect(object_1.left + 2, object_1.bottom, object_1.width-2, 2)
    inside = Rect(object_1.left + 2, object_1.top + 2, object_1.width - 4, object_1.height - 2)
    if left.colliderect(object_2):
        l = True
    if right.colliderect(object_2):
        r = True
    if top.colliderect(object_2):
        t = True
    if bottom.colliderect(object_2):
        b = True
    if inside.colliderect(object_2):
        i = True # object_1 is inside object_2
    return (t,r,b,l,i)

#def process_obstacle_coords(level_dir, level_name):
def process_obstacle_coords(xyfilename):
    try:
#        xyfile = open(level_dir + level_name + '/obstacle_coords_file_' + level_name + '.txt')
#        xyfile = open(level_dir + level_name + '/obstacle_coords_file_' + level_name + '.txt')
        xyfile = open(xyfilename)
    except IOError:
        print("ERROR: %s not found" % xyfilename)
#        print(level_dir + level_name + "/" + "obstacle_coords_file_" + level_name + ".txt not found")
        raise IOError
    obstacle_list = []
    for line in xyfile:
        line = line.strip()
        line = line.split(",")
        obstacle_list.append(line)
    return obstacle_list

#def process_enemy_coords(level_dir, level_name):
def process_enemy_coords(xyfilename):
    enemyfile = open(xyfilename)
#    enemyfile = open(level_dir + level_name + "/" + "enemy_coords_file_" + level_name + ".txt")
    enemy_list = []
    for line in enemyfile:
        line = line.strip()
        line = line.split(",")
        enemy_list.append(line)
    return enemy_list 
